package org.example.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.Dao.GoodsDao;
import org.example.Pojo.Goods;
import org.example.Service.GoodsService;
import org.example.Utils.UnionResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    GoodsDao goodsDao;
    @Override
    public UnionResult addGoods(Goods goods) {
        String[] strs = {"1.jpg","2.jpg","3.jpg","4.jpg","5.jpg"};
        Random random = new Random();
        int randomNumber = random.nextInt(5);
        goods.setImgid(strs[randomNumber]);
        if (goodsDao.insert(goods) > 0) {
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult updateGoods(Goods goods) {
        if (goodsDao.updateById(goods) > 0) {
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult deleteGoods(Goods goods) {

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",goods.getUid());
        if (goodsDao.delete(queryWrapper) > 0) {
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult getAllGoods() {
        return UnionResult.Success("",
                goodsDao.selectList(new QueryWrapper<Goods>()));
    }

    @Override
    public UnionResult QueryGoodsByName(String goodsName) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("goodsname",goodsName);
        return UnionResult.Success("",goodsDao.selectList(queryWrapper));
    }
}
